package com.wolfalpha.utron.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.wolfalpha.utron.R;

/**
 * 自定义的对话框
 *
 * @author
 */
public class EditAlertDialog {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView sure;
    private TextView cancel;
    private EditText etContent;
    private TextView left;

    private LayoutInflater inflater;

    private int mostLength = 50;

    public EditAlertDialog(Context context) {
        // TODO Auto-generated constructor stub

        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.requestWindowFeature(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        ad.setView(inflater.inflate(R.layout.dialog_edit, null));
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_edit);
        sure = (TextView) window.findViewById(R.id.tv_dialog_base_sure);
        cancel = (TextView) window.findViewById(R.id.tv_dialog_base_cancel);
        etContent = (EditText) window.findViewById(R.id.et_dialog_input);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                left.setText(String.valueOf(etContent.length()) + "/" + String.valueOf(mostLength));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        left = (TextView) window.findViewById(R.id.tv_edit_words_left);
        left.setText("0/" + String.valueOf(mostLength));

        setNegativeButton("取消");
        setPositiveButton("确认");
    }


    /**
     * 设置按钮
     *
     * @param text
     */
    public void setPositiveButton(String text) {
        sure.setText(text);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.finish(etContent.getText().toString());
                }
                dismiss();
            }
        });

    }


    /**
     * 设置最长字
     *
     * @param max
     */
    public void setMostWords(int max) {
        etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
        this.mostLength = max;
    }

    /**
     * 设置提示
     *
     * @param hint
     */
    public void setHint(String hint) {
        etContent.setHint(hint);
    }

    /**
     * 设置按钮
     *
     * @param text
     */
    public void setNegativeButton(String text) {
        cancel.setText(text);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

    private OnFinishedListener mListener;

    public void setOnFinishedListener(OnFinishedListener listener) {
        mListener = listener;
    }

    public interface OnFinishedListener {
        public void finish(String input);
    }

}